<template>
  <div class="bartendingOrderList">
    <template v-if="orderItem.length > 0">
      <div class="list" v-for="item in orderItem" :key="item.id" @click="toOrderDetail(item)">
        <div class="list_top">
          <div class="list_top_tag">{{ item.dispatch_type_name }}</div>
          <div class="list_top_od">订单号：{{ item.order_sn }}</div>
          <div class="list_top_status">{{ item.status_name }}</div>
        </div>
        <goodList listType="order" :barrelList="item.has_many_order_goods"></goodList>
        <div class="list_user" v-if="showUser && item.belongs_to_member">
          <img :src="item.belongs_to_member.avatar_image" class="list_user_img" alt="" />
          <div class="list_user_txt">{{ item.belongs_to_member.nickname }} {{ item.belongs_to_member.mobile }}</div>
        </div>
        <div class="payNum">实付：{{ $i18n.t("money") }}{{ item.price }}</div>
        <div class="btnArr">
          <div class="btn" v-for="(items, index) in item.button_models" @click.stop="operation(items, item)" :key="index">{{ items.name }}</div>
        </div>
      </div>
    </template>
    <orderComfirmDelivery ref="orderComfirmDelivery" @closePopup="close" @updateData="updateDataMethods"></orderComfirmDelivery>
  </div>
</template>
<script>
import goodList from "./goodList.vue";
import orderComfirmDelivery from "./orderComfirmDelivery.vue";
export default {
  props: {
    orderItem: {
      type: null
    },
    showUser: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
    };
  },
  mounted() {},
  methods: {
    updateDataMethods() {
      this.$emit("updateData");
    },
    close() {
      this.visibleComfirm = false;
    },
    toOrderDetail(item) {
      this.$router.push(this.fun.getUrl("orderdetail", { order_id: item.id, orderType: "selfBartending" }, {}));
    },
    operation(btn, order) {
      try {
        this.orderPayId = order.id || this.$route.params.order_id;
        this.buttonParamsData = { order: order, btn: btn };
      } catch (error) {
        console.log(error);
      }
      let _buttonType = btn.value; //当前的按钮类型
      let __SET_DATA = {};
      let _NAVIGATE_TO = new Map([
        [1, { name: "orderpay", params: { status: 1, order_ids: order.id }, query: { api: btn.api } }],
        ["verification_code", { name: "orderVerification", params: { order_id: order.id, api: btn.api } }], // 验证核销
        [3, { name: "OrderReturn", params: { id: order.id } }]
        //  ... 更多
      ]);
      // 先通过has()判断_NAVIGATE_TO 是否包含 btn.value
      if (_NAVIGATE_TO.has(btn.value)) {
        // 有，说明该 btn.value 是跳转页面的按钮
        _buttonType = "toUrl";
        __SET_DATA = _NAVIGATE_TO.get(btn.value);
      } else {
        // .....更多其他场景补充
      }
      // _GET_MAP_DATA 设置btn.value,对应的方法和参数
      const _GET_MAP_DATA = new Map([
        ["toUrl", ["goToUrl", __SET_DATA]], //通过$router.push跳转页面的处理
        [9, ["publicMessage", { type: "refresh" }]], //取消订单，type="back" 成功后返回上一页
        [12, ["publicMessage", { type: "refresh" }]], //删除订单，type="back" 成功后返回上一页
        [5, ["publicMessage", { type: "refresh" }]] //确认收货
        // .....更多补充
      ]);
      if (_GET_MAP_DATA.has(_buttonType)) {
        // 如果_GET_MAP_DATA 常量中存在了当前的 btn.value，即走Map结构设置的
        let _GET_CONFIG = _GET_MAP_DATA.get(_buttonType);
        let _METHODS_NAME = _GET_CONFIG[0] || ""; //取 方法名
        let _PARAMS_DATA = _GET_CONFIG[1] || {}; // 取 设置的参数
        console.log("_METHODS_NAME", _METHODS_NAME, _PARAMS_DATA);
        this[_METHODS_NAME](_PARAMS_DATA); //调用方法并传参
      } else if (btn.value == "self_bartending_again" || btn.value == "self_bartending_order_send") {
        this.selfBartending_nextPost(btn, order);
      } else {
        // 其他，走publicMessage默认流程：Dialog提示框显示=》确认按钮=》请求接口成功=》刷新页面或返回上一页...
        this.publicMessage();
      }
      // if (item.value == 5) {
      //   // 确认发货
      //   this.$refs.orderComfirmDelivery.init(order.id, order.address);
      //   this.visibleComfirm = true;
      //   return;
      // }
      // this.nextPost(item, order);
    },
    async selfBartending_nextPost(btn, order) {
      let msgTxt = "是否再次调酒?";
      if (btn.value == "self_bartending_order_send") {
        this.$refs.orderComfirmDelivery.init(order.id,order.address);
        return;
      }
      this.$dialog
        .confirm({
          message: msgTxt
        })
        .then(async () => {
          if (btn.value == "self_bartending_again") {
            let { data, msg, result } = await $http.post(btn.api, { order_id: order.id }, ".");
            if (result) {
              this.$router.push(this.fun.getUrl("goodsorder", {}, { temp_id: data.id, tag: "selfBartendBuy" }));
            } else {
              this.$toast(msg);
            }
          }
        });
    },
    publicMessage(option = {}) {
      // 流程：Dialog提示框显示=》确认按钮=》请求接口成功=》刷新或者跳转页面...
      let { order = {}, btn = {} } = this.buttonParamsData; //{order:{...}, btn:{...}}
      let { api = "", text = "", type = "refresh", json = {} } = option; //type: refresh刷新页面&&back返回上一页
      let order_id = this.orderPayId;
      const REQUESE_Data = { order_id, ...json }; //接口请求参数
      const TEXT = text || btn.name; //可自定义text参数，提示框提示文字，默认值为接口button_models里的name字段
      const REQUESE_URL = api || btn.api; //可自定义api参数，按钮点击请求事件的接口，默认值为接口button_models里的api字段

      if (!REQUESE_URL) {
        console.log("error：：publicMessage方法出错，当前按钮接口没有返回api，请和后端人员确认");
        return;
      }

      if ((order.plugin_id == 0 || order.plugin_id == 44) && btn.value == 5 && this.custom_receipt_confirmation_html !== "") {
        this.custom_receipt_confirmation_show = true;
        return;
      }

      this.$dialog
        .confirm({ message: `您确定${TEXT}？` })
        .then(() => {
          if (this.noMoreClick) {
            return;
          }
          this.noMoreClick = true;
          $http.get(REQUESE_URL, REQUESE_Data).then(
            response => {
              this.noMoreClick = false;
              if (response.result == 1) {
                this.$notify({
                  background: "#f0f9eb",
                  message: `${TEXT}成功`,
                  color: "#67c23a"
                });
                if (order.is_boss) {
                  // is_boss：true是店长,店长跳转门店订单列表页
                  this.$router.push(this.fun.getUrl("orderlist", { orderType: "store", status: "0" }));
                  return;
                }
                if (type == "refresh") {
                  this.$emit("refreshUpdate");
                } else if (type == "back") {
                  // 返回上一页
                  if (window.history.length <= 1) {
                    this.$router.push(this.fun.getUrl("home", {}));
                  } else {
                    this.$router.go(-1);
                  }
                }
              } else {
                this.$dialog.alert({ message: response.msg });
              }
            },
            response => {
              this.noMoreClick = false;
              console.log(response);
            }
          );
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async nextPost(item, order) {
      let valmsg = "";
      if (item.value == 6) {
        valmsg = "您确定核销";
      }
      this.$dialog
        .confirm({
          // title: `${valmsg ? valmsg : "您确定" + item.name}?`,
          message: `${valmsg ? valmsg : "您确定" + item.name}?`
        })
        .then(async () => {
          let { data, msg, result } = await $http.post(item.api, { order_id: order.id }, ".");
          if (result) {
            if (item.value == 4) {
              // 再次调酒
              this.$router.push(this.fun.getUrl("goodsorder", {}, { temp_id: data.id, tag: "selfBartendBuy" }));
              return;
            }
            this.$toast(msg);
          } else {
            this.$toast(msg);
          }
        })
        .catch(() => {});
      console.log(item.api, item.name, order);
    }
  },
  components: { goodList, orderComfirmDelivery }
};
</script>
<style lang="scss" scoped>
.bartendingOrderList {
  .list:last-child {
    margin-bottom: 0;
    margin-top: 0.44rem;
  }
  .list {
    padding: 0.94rem 0.75rem;
    margin-bottom: 0.63rem;
    background: #fff;
    border-radius: 0.5rem;
    .btnArr {
      margin-top: 0.94rem;
      display: flex;
      justify-content: flex-end;
      align-items: center;
      flex-wrap: wrap;
      .btn {
        border-radius: 1.88rem;
        border: 0.03rem solid #aaaab3;
        font-size: 0.81rem;
        color: #00001c;
        padding: 0.59rem 0.78rem;
        box-sizing: border-box;
        margin-left: 0.5rem;
      }
      .btn:first-child {
        margin-left: 0;
      }
    }
    .payNum {
      display: flex;
      align-items: center;
      justify-content: flex-end;
      font-weight: bold;
      font-size: 0.88rem;
      margin-top: 0.94rem;
    }
    .list_user {
      display: flex;
      justify-content: flex-start;
      align-items: center;
      .list_user_img {
        width: 1.5rem;
        height: 1.5rem;
        background: #d6d6dc;
        border-radius: 2.69rem;
        margin: 0;
        margin-right: 0.25rem;
      }
      .list_user_txt {
        font-size: 0.88rem;
        color: #999999;
      }
    }

    .list_top {
      display: flex;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 0.94rem;
      .list_top_tag {
        height: 18px;
        font-size: 12px;
        color: #ffffff;
        padding: 0 0.31rem;
        box-sizing: border-box;
        background: #f15353;
        border-radius: 0.25rem;
        margin-right: 0.25rem;
      }
      .list_top_od {
        flex: 1;
        font-weight: 500;
        font-size: 0.81rem;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        text-align: left;
      }
      .list_top_status {
        flex-shrink: 0;
        font-size: 0.88rem;
        color: #f15353;
      }
    }
  }
}
</style>
